#include "StdAfx.h"
#include "DataPool.h"
#include "Lobby.h"
#include "MainFrm.h"

CDataPool g_dataPool;

int compProductGameId(const void *pElement1,const void *pElement2)
{
	ProductGameId *pInfo1 = (ProductGameId *)pElement1;
	ProductGameId *pInfo2 = (ProductGameId *)pElement2;
	return pInfo1->dwProductId - pInfo2->dwProductId;
}

int compUserSignupInfo(const void *arg1,const void *arg2)
{
	UserSignupInfo *pInfo1 = (UserSignupInfo*)arg1;
	UserSignupInfo *pInfo2 = (UserSignupInfo*)arg2;

	if (pInfo1->dwProductId == pInfo2->dwProductId)
	{
		return (int)pInfo1->tmMatchStartTime - (int)pInfo2->tmMatchStartTime;
	}	
	return (int)pInfo1->dwProductId - (int)pInfo2->dwProductId;
}

CDataPool::CDataPool(void)
{
	memset(m_szPwd, 0, sizeof(m_szPwd));
	memset(m_szUserName, 0, sizeof(m_szUserName));
	m_dwUserId = 0;
	m_nMoney = 0;
	m_nExp = 0;
	m_nLevel = 0;
	m_nFigure = 0;
}

CDataPool::~CDataPool(void)
{
}

void CDataPool::SetUserInfo(LCUserLoginMsg *pMsg)
{
    m_dwUserId = pMsg->m_dwUserId;
	strncpy(m_szUserName, pMsg->m_szUserName, sizeof(m_szUserName));
	m_nMoney = pMsg->m_nMoney;
	m_nExp = pMsg->m_nExp;
	m_nLevel = pMsg->m_nLevel;
    m_nFigure = pMsg->m_nFigure;
}

BOOL CDataPool::AddSignupInfo(DWORD dwProductId, DWORD tmMatchStartTime)
{
	UserSignupInfo info;
	info.dwProductId = dwProductId;
	info.tmMatchStartTime = tmMatchStartTime;
	UserSignupInfo *pInfo = m_listSignupInfo.Find(info, compUserSignupInfo);
	if(pInfo == NULL)
	{
		m_listSignupInfo.Insert(info, compUserSignupInfo);
	}
	return TRUE;
}

BOOL CDataPool::DelSignupInfo(DWORD dwProductId, DWORD tmMatchStartTime)
{
	UserSignupInfo info;
	info.dwProductId = dwProductId;
	info.tmMatchStartTime = tmMatchStartTime;
	m_listSignupInfo.Remove(info, compUserSignupInfo);
	return TRUE;
}

DWORD CDataPool::GetProductGameId(DWORD dwProductId)
{
	ProductGameId temp;
	temp.dwProductId = dwProductId;
	ProductGameId *pProductGameId = m_listProductGameId.Find(temp, compProductGameId);
    if(pProductGameId == NULL)
		return 0;
	return pProductGameId->dwGameId;
}

void CDataPool::SetProductGameId(DWORD dwProductId, DWORD dwGameId)
{
	ProductGameId temp;
	temp.dwProductId = dwProductId;
	temp.dwGameId = dwGameId;
	ProductGameId *pProductGameId = m_listProductGameId.Find(temp, compProductGameId);
	if(pProductGameId == NULL)
	{
        m_listProductGameId.Insert(temp, compProductGameId);
	}
}

